package com.isoft.student.service.impl;

import com.isoft.student.dao.ExampaperMapper;
import com.isoft.student.dao.TestpaperMapper;
import com.isoft.student.eo.ExampaperEOPage;
import com.isoft.student.model.Exampaper;
import com.isoft.student.service.ExampaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 实现业务及流程接口当中的内容
 */
@Service
public class ExampaperServiceImpl implements ExampaperService {

    @Autowired
    private ExampaperMapper exampaperMapper;
    @Autowired
    private TestpaperMapper testpaperMapper;

    /**
     * 答题表添加方式实现
     * @param exampaper 答题
     * @return 是否成功（-1 重复添加）
     */
    @Override
    public int AddExampaper(Exampaper exampaper){
        int returnInt=0;
        //业务判断
        //（1）答题名称不能重复

        //(1)判断答题
        int TestpaperidCount=exampaperMapper.getTestpaperidCount(exampaper.getTestpaperid());
        if (TestpaperidCount<1){
            //2.答题信息数据添加
            int addExampaperInt=exampaperMapper.insertExampaper(exampaper);
            //3.返回值设置
            returnInt=addExampaperInt;
        }else {
            returnInt=-1;//有相同的信息名称
        }

        return returnInt;
    }

    /**
     * 答题更新
     * @param exampaper 答题
     * @return 更新是否成功（-1：更新执行SQL报错）
     */

    @Override
    public int UpdateExampaper(Exampaper exampaper){
        int isUpdate=0;
        //1.没有相关业务流程的判断
        try {
            isUpdate=exampaperMapper.updateExampaper(exampaper);
        }catch (Exception e){
            isUpdate=-1;
            //打印更新报错的情况
            System.out.print("更新信息时执行SQL报错的原因为："+e.getMessage());
        }
        return isUpdate;
    }

    /**
     * 试卷考题删除
     * @param testpaperid
     * @return
     */
    @Override
    public int DeleteExampaper(int testpaperid) {
        int delExampaper=0;
        /*1.有关业务流程的判断
         * （1）删除试卷考题不能与试卷表有关联
         * */
        try {
            //返回的结果总数量小于1，才可以删除
            if (testpaperMapper.getExampaperCount(testpaperid)>0){
                delExampaper=exampaperMapper.delExampaper(testpaperid);
            }
        }catch (Exception e){
            delExampaper=-1;
            System.out.print("删除角色信息室执行SQL时报错，信息为："+e.getMessage());
        }
        return delExampaper;
    }

    @Override
    public List<ExampaperEOPage> getExampaperWhereAll(ExampaperEOPage exampaperEOPage) {
        List<ExampaperEOPage> listExampaperEo=exampaperMapper.getExampaperWhereAll(exampaperEOPage);
        return listExampaperEo;
    }
}